home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / ntpath.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-10-18  |  11.6 KB  |  483 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. '''Common pathname manipulations, WindowsNT/95 version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. import sys
  12. __all__ = [
  13.     'normcase',
  14.     'isabs',
  15.     'join',
  16.     'splitdrive',
  17.     'split',
  18.     'splitext',
  19.     'basename',
  20.     'dirname',
  21.     'commonprefix',
  22.     'getsize',
  23.     'getmtime',
  24.     'getatime',
  25.     'getctime',
  26.     'islink',
  27.     'exists',
  28.     'lexists',
  29.     'isdir',
  30.     'isfile',
  31.     'ismount',
  32.     'walk',
  33.     'expanduser',
  34.     'expandvars',
  35.     'normpath',
  36.     'abspath',
  37.     'splitunc',
  38.     'curdir',
  39.     'pardir',
  40.     'sep',
  41.     'pathsep',
  42.     'defpath',
  43.     'altsep',
  44.     'extsep',
  45.     'devnull',
  46.     'realpath',
  47.     'supports_unicode_filenames']
  48. curdir = '.'
  49. pardir = '..'
  50. extsep = '.'
  51. sep = '\\'
  52. pathsep = ';'
  53. altsep = '/'
  54. defpath = '.;C:\\bin'
  55. if 'ce' in sys.builtin_module_names:
  56.     defpath = '\\Windows'
  57. elif 'os2' in sys.builtin_module_names:
  58.     altsep = '/'
  59.  
  60. devnull = 'nul'
  61.  
  62. def normcase(s):
  63.     '''Normalize case of pathname.
  64.  
  65.     Makes all characters lowercase and all slashes into backslashes.'''
  66.     return s.replace('/', '\\').lower()
  67.  
  68.  
  69. def isabs(s):
  70.     '''Test whether a path is absolute'''
  71.     s = splitdrive(s)[1]
  72.     if s != '':
  73.         pass
  74.     return s[:1] in '/\\'
  75.  
  76.  
  77. def join(a, *p):
  78.     '''Join two or more pathname components, inserting "\\" as needed'''
  79.     path = a
  80.     for b in p:
  81.         b_wins = 0
  82.         if path == '':
  83.             b_wins = 1
  84.         elif isabs(b):
  85.             if path[1:2] != ':' or b[1:2] == ':':
  86.                 b_wins = 1
  87.             elif (len(path) > 3 or len(path) == 3) and path[-1] not in '/\\':
  88.                 b_wins = 1
  89.             
  90.         
  91.         if b_wins:
  92.             path = b
  93.             continue
  94.         if path[-1] in '/\\':
  95.             if b and b[0] in '/\\':
  96.                 path += b[1:]
  97.             else:
  98.                 path += b
  99.         b[0] in '/\\'
  100.         if path[-1] == ':':
  101.             path += b
  102.             continue
  103.         if b:
  104.             if b[0] in '/\\':
  105.                 path += b
  106.             else:
  107.                 path += '\\' + b
  108.         b[0] in '/\\'
  109.         path += '\\'
  110.     
  111.     return path
  112.  
  113.  
  114. def splitdrive(p):
  115.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  116. "(drive,path)";  either part may be empty'''
  117.     if p[1:2] == ':':
  118.         return (p[0:2], p[2:])
  119.     
  120.     return ('', p)
  121.  
  122.  
  123. def splitunc(p):
  124.     """Split a pathname into UNC mount point and relative path specifiers.
  125.  
  126.     Return a 2-tuple (unc, rest); either part may be empty.
  127.     If unc is not empty, it has the form '//host/mount' (or similar
  128.     using backslashes).  unc+rest is always the input path.
  129.     Paths containing drive letters never have an UNC part.
  130.     """
  131.     if p[1:2] == ':':
  132.         return ('', p)
  133.     
  134.     firstTwo = p[0:2]
  135.     if firstTwo == '//' or firstTwo == '\\\\':
  136.         normp = normcase(p)
  137.         index = normp.find('\\', 2)
  138.         if index == -1:
  139.             return ('', p)
  140.         
  141.         index = normp.find('\\', index + 1)
  142.         if index == -1:
  143.             index = len(p)
  144.         
  145.         return (p[:index], p[index:])
  146.     
  147.     return ('', p)
  148.  
  149.  
  150. def split(p):
  151.     '''Split a pathname.
  152.  
  153.     Return tuple (head, tail) where tail is everything after the final slash.
  154.     Either part may be empty.'''
  155.     (d, p) = splitdrive(p)
  156.     i = len(p)
  157.     while i and p[i - 1] not in '/\\':
  158.         i = i - 1
  159.     head = p[:i]
  160.     tail = p[i:]
  161.     head2 = head
  162.     while head2 and head2[-1] in '/\\':
  163.         head2 = head2[:-1]
  164.     if not head2:
  165.         pass
  166.     head = head
  167.     return (d + head, tail)
  168.  
  169.  
  170. def splitext(p):
  171.     '''Split the extension from a pathname.
  172.  
  173.     Extension is everything from the last dot to the end.
  174.     Return (root, ext), either part may be empty.'''
  175.     i = p.rfind('.')
  176.     if i <= max(p.rfind('/'), p.rfind('\\')):
  177.         return (p, '')
  178.     else:
  179.         return (p[:i], p[i:])
  180.  
  181.  
  182. def basename(p):
  183.     '''Returns the final component of a pathname'''
  184.     return split(p)[1]
  185.  
  186.  
  187. def dirname(p):
  188.     '''Returns the directory component of a pathname'''
  189.     return split(p)[0]
  190.  
  191.  
  192. def commonprefix(m):
  193.     '''Given a list of pathnames, returns the longest common leading component'''
  194.     if not m:
  195.         return ''
  196.     
  197.     prefix = m[0]
  198.     for item in m:
  199.         for i in range(len(prefix)):
  200.             if prefix[:i + 1] != item[:i + 1]:
  201.                 prefix = prefix[:i]
  202.                 if i == 0:
  203.                     return ''
  204.                 
  205.                 break
  206.                 continue
  207.         
  208.     
  209.     return prefix
  210.  
  211.  
  212. def getsize(filename):
  213.     '''Return the size of a file, reported by os.stat()'''
  214.     return os.stat(filename).st_size
  215.  
  216.  
  217. def getmtime(filename):
  218.     '''Return the last modification time of a file, reported by os.stat()'''
  219.     return os.stat(filename).st_mtime
  220.  
  221.  
  222. def getatime(filename):
  223.     '''Return the last access time of a file, reported by os.stat()'''
  224.     return os.stat(filename).st_atime
  225.  
  226.  
  227. def getctime(filename):
  228.     '''Return the creation time of a file, reported by os.stat().'''
  229.     return os.stat(filename).st_ctime
  230.  
  231.  
  232. def islink(path):
  233.     '''Test for symbolic link.  On WindowsNT/95 always returns false'''
  234.     return False
  235.  
  236.  
  237. def exists(path):
  238.     '''Test whether a path exists'''
  239.     
  240.     try:
  241.         st = os.stat(path)
  242.     except os.error:
  243.         return False
  244.  
  245.     return True
  246.  
  247. lexists = exists
  248.  
  249. def isdir(path):
  250.     '''Test whether a path is a directory'''
  251.     
  252.     try:
  253.         st = os.stat(path)
  254.     except os.error:
  255.         return False
  256.  
  257.     return stat.S_ISDIR(st.st_mode)
  258.  
  259.  
  260. def isfile(path):
  261.     '''Test whether a path is a regular file'''
  262.     
  263.     try:
  264.         st = os.stat(path)
  265.     except os.error:
  266.         return False
  267.  
  268.     return stat.S_ISREG(st.st_mode)
  269.  
  270.  
  271. def ismount(path):
  272.     '''Test whether a path is a mount point (defined as root of drive)'''
  273.     (unc, rest) = splitunc(path)
  274.     if unc:
  275.         return rest in ('', '/', '\\')
  276.     
  277.     p = splitdrive(path)[1]
  278.     if len(p) == 1:
  279.         pass
  280.     return p[0] in '/\\'
  281.  
  282.  
  283. def walk(top, func, arg):
  284.     """Directory tree walk with callback function.
  285.  
  286.     For each directory in the directory tree rooted at top (including top
  287.     itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
  288.     dirname is the name of the directory, and fnames a list of the names of
  289.     the files and subdirectories in dirname (excluding '.' and '..').  func
  290.     may modify the fnames list in-place (e.g. via del or slice assignment),
  291.     and walk will only recurse into the subdirectories whose names remain in
  292.     fnames; this can be used to implement a filter, or to impose a specific
  293.     order of visiting.  No semantics are defined for, or required of, arg,
  294.     beyond that arg is always passed to func.  It can be used, e.g., to pass
  295.     a filename pattern, or a mutable object designed to accumulate
  296.     statistics.  Passing None for arg is common."""
  297.     
  298.     try:
  299.         names = os.listdir(top)
  300.     except os.error:
  301.         return None
  302.  
  303.     func(arg, top, names)
  304.     exceptions = ('.', '..')
  305.     for name in names:
  306.         if name not in exceptions:
  307.             name = join(top, name)
  308.             if isdir(name):
  309.                 walk(name, func, arg)
  310.             
  311.         isdir(name)
  312.     
  313.  
  314.  
  315. def expanduser(path):
  316.     '''Expand ~ and ~user constructs.
  317.  
  318.     If user or $HOME is unknown, do nothing.'''
  319.     if path[:1] != '~':
  320.         return path
  321.     
  322.     i = 1
  323.     n = len(path)
  324.     while i < n and path[i] not in '/\\':
  325.         i = i + 1
  326.     if i == 1:
  327.         if 'HOME' in os.environ:
  328.             userhome = os.environ['HOME']
  329.         elif 'HOMEPATH' not in os.environ:
  330.             return path
  331.         else:
  332.             
  333.             try:
  334.                 drive = os.environ['HOMEDRIVE']
  335.             except KeyError:
  336.                 drive = ''
  337.  
  338.             userhome = join(drive, os.environ['HOMEPATH'])
  339.     else:
  340.         return path
  341.     return userhome + path[i:]
  342.  
  343.  
  344. def expandvars(path):
  345.     '''Expand shell variables of form $var and ${var}.
  346.  
  347.     Unknown variables are left unchanged.'''
  348.     if '$' not in path:
  349.         return path
  350.     
  351.     import string
  352.     varchars = string.ascii_letters + string.digits + '_-'
  353.     res = ''
  354.     index = 0
  355.     pathlen = len(path)
  356.     while index < pathlen:
  357.         c = path[index]
  358.         if c == "'":
  359.             path = path[index + 1:]
  360.             pathlen = len(path)
  361.             
  362.             try:
  363.                 index = path.index("'")
  364.                 res = res + "'" + path[:index + 1]
  365.             except ValueError:
  366.                 res = res + path
  367.                 index = pathlen - 1
  368.             except:
  369.                 None<EXCEPTION MATCH>ValueError
  370.             
  371.  
  372.         None<EXCEPTION MATCH>ValueError
  373.         if c == '$':
  374.             if path[index + 1:index + 2] == '$':
  375.                 res = res + c
  376.                 index = index + 1
  377.             elif path[index + 1:index + 2] == '{':
  378.                 path = path[index + 2:]
  379.                 pathlen = len(path)
  380.                 
  381.                 try:
  382.                     index = path.index('}')
  383.                     var = path[:index]
  384.                     if var in os.environ:
  385.                         res = res + os.environ[var]
  386.                 except ValueError:
  387.                     res = res + path
  388.                     index = pathlen - 1
  389.                 except:
  390.                     None<EXCEPTION MATCH>ValueError
  391.                 
  392.  
  393.             None<EXCEPTION MATCH>ValueError
  394.             var = ''
  395.             index = index + 1
  396.             c = path[index:index + 1]
  397.             while c != '' and c in varchars:
  398.                 var = var + c
  399.                 index = index + 1
  400.                 c = path[index:index + 1]
  401.             if var in os.environ:
  402.                 res = res + os.environ[var]
  403.             
  404.             if c != '':
  405.                 res = res + c
  406.             
  407.         else:
  408.             res = res + c
  409.         index = index + 1
  410.     return res
  411.  
  412.  
  413. def normpath(path):
  414.     '''Normalize path, eliminating double slashes, etc.'''
  415.     path = path.replace('/', '\\')
  416.     (prefix, path) = splitdrive(path)
  417.     if prefix == '':
  418.         while path[:1] == '\\':
  419.             prefix = prefix + '\\'
  420.             path = path[1:]
  421.     elif path.startswith('\\'):
  422.         prefix = prefix + '\\'
  423.         path = path.lstrip('\\')
  424.     
  425.     comps = path.split('\\')
  426.     i = 0
  427.     while i < len(comps):
  428.         if comps[i] in ('.', ''):
  429.             del comps[i]
  430.             continue
  431.         if comps[i] == '..':
  432.             if i > 0 and comps[i - 1] != '..':
  433.                 del comps[i - 1:i + 1]
  434.                 i -= 1
  435.             elif i == 0 and prefix.endswith('\\'):
  436.                 del comps[i]
  437.             else:
  438.                 i += 1
  439.         prefix.endswith('\\')
  440.         i += 1
  441.     if not prefix and not comps:
  442.         comps.append('.')
  443.     
  444.     return prefix + '\\'.join(comps)
  445.  
  446.  
  447. def abspath(path):
  448.     '''Return the absolute version of a path'''
  449.     global abspath
  450.     
  451.     try:
  452.         _getfullpathname = _getfullpathname
  453.         import nt
  454.     except ImportError:
  455.         
  456.         def _abspath(path):
  457.             if not isabs(path):
  458.                 path = join(os.getcwd(), path)
  459.             
  460.             return normpath(path)
  461.  
  462.         abspath = _abspath
  463.         return _abspath(path)
  464.  
  465.     if path:
  466.         
  467.         try:
  468.             path = _getfullpathname(path)
  469.         except WindowsError:
  470.             pass
  471.         except:
  472.             None<EXCEPTION MATCH>WindowsError
  473.         
  474.  
  475.     None<EXCEPTION MATCH>WindowsError
  476.     path = os.getcwd()
  477.     return normpath(path)
  478.  
  479. realpath = abspath
  480. if hasattr(sys, 'getwindowsversion'):
  481.     pass
  482. supports_unicode_filenames = sys.getwindowsversion()[3] >= 2
  483.